package controllers.api;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.PersistenceException;

import models.MBao;

import org.nutz.json.Json;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import play.Logger;
import play.mvc.Controller;

public class Reciever extends Controller {
	/**
	 * 接收
	 */
	public static void g(String json, String pass){
//		if(! "p".equals(pass)){
//			return ;
//		}
		try{
			List baos = Json.fromJson(ArrayList.class, json);
			for(Object o : baos){
				MBao bao = (MBao) Json.fromJson(MBao.class, Json.toJson(o));
//				System.out.println(bao.price);
//				System.out.println(bao.title);
				if(MBao.valid(bao)){
					try{
						bao.save();
					} catch(PersistenceException e){
						System.out.println("DUP:" + e.getClass().getName() + "\n MES:" + e.getMessage());
						MBao oriBao = MBao.find("title=?", bao.title).first();
						oriBao.score = bao.score;
						oriBao.save();
						//bao.
					}
				}
			}
		} catch(Throwable e){
			Logger.error(e, "Fail to parse string: ", json);
		}
	}
	
}
